
Ext.namespace('business.equipment');

	// 主页面
	business.equipment.MailViewer = Ext.extend(Ext.Panel, {
		
		/**
		 * 左侧变电站树
		 */ 
		bdztree  : null,
		
		/**
		 * 右侧设备列表
		 */ 
		sb       : null,
		
		border   : false,

		/**
		 * 变电站ID
		 */ 
		bdzid    : '0',
		bdzname  : '',

		/**
		 * 间隔或屏柜ID
		 */ 
		typeid   : '0',
		typename : '',

		/**
		 * 间隔或屏柜状态
		 */ 
		state    : '0',
		
		/**
	     * 初始化
		 */
		initComponent : function() {
			
			var obj = this;
			
			this.addListener('render', function() {
				obj.startViewer.defer(1, obj);
			});

			this.layout	= 'fit';
			
			business.equipment.MailViewer.superclass.initComponent.call(this);
		},
		
		/**
		 * 初始化顺序处理
		 */
		startViewer : function() {
			
			// 初始化页面
			this.renderViewer();
			// grid绑定菜单
			this.sb.menuCreate();

		},
		
		/**
		 * 加载控件
		 */
		renderViewer : function() {

			var obj = this;

			//----------------------------------------------------------------------
			//	变电站树生成
			//----------------------------------------------------------------------
			this.bdztree = new business.equipment.BdzTree({
				region				: 'west',
				layout              : 'fit',
				border              : false,
				split				: true,
				collapsible			: true,
				animCollapse		: false,
				width				: 200,    
                minSize				: 175,   
                maxSize				: 250,
                title               : '变电站列表'
               
			});
			
			//----------------------------------------------------------------------
			//	变电站树事件 
			//----------------------------------------------------------------------
			this.bdztree.on({
				
				// 单击时
				'loadGrid' : function (node) {

					// 变电站
					 if (node.id.toString().indexOf('bdz') != -1) {
						
						obj.bdzid = node.id.toString().replace('bdz','');
						obj.typeid = '0';
						obj.state = '0';
						obj.fireEvent('loadGrid');
						
						// 隔断或者屏柜
					} else if (node.id.toString() != '0') {
						obj.bdzid = node.parentNode.id.toString().replace('bdz','');
						obj.bdzname = node.parentNode.text;
						obj.typeid = node.id;
						obj.typename = node.text;
						obj.state = node.attributes.state;
						obj.fireEvent('loadGrid');
					}

					obj.sb.gridstore.baseParams['searchBean.con2'] = obj.bdzid;
					obj.sb.gridstore.baseParams['searchBean.con3'] = obj.typeid;
					obj.sb.gridstore.reload();
				},
				
				'resetGrid' : function (node) {
					obj.sb.gridstore.removeAll();
				}
				
			})

			//----------------------------------------------------------------------
			//	设备列表生成
			//----------------------------------------------------------------------
			this.sb = new business.equipment.BdzSb({
				region				: 'center',
				//layout              : 'fit',
              	activeTab           : 0
			});
			
			//----------------------------------------------------------------------
			//	设备事件 
			//----------------------------------------------------------------------
			this.sb.on({
				
				// 新建
				'addTab' : function (sblx, sblxname, jsp) {
					alert(typeid);
					if (typeid == '0') {
						alert();
						Ext.MessageBox.alert('提示', '新建时请先选择隔断或者屏柜');
						return;
					}
					
					var url = contextpath + '/jsp/business/equipment/' + jsp
									+ '?bdzid=' + obj.bdzid + '&state=' + obj.state + '&bdzname=' + obj.bdzname + '&typeid=' + obj.typeid + '&typename=' + obj.typename + '&sblx=' + sblx + '&sblxname=' + sblxname;

					obj.sb.add({
						id       : sblx + '_0',
						closable : true,
						html     : "<div scrolling='no' style= 'overflow:hidden'><iframe id='" + sblx + "_0' src= '" + url + "' width='100%' height='100%' frameborder='0' scrolling='no'></iframe></div>",
						layout   : 'fit',
						title    : sblxname
					})
					
					obj.sb.setActiveTab(obj.sb.getItem(sblx + '_0'));

				}

			})

			var viewer = ({
				layout          : 'border',
				border			: false,
				items           : [obj.bdztree, obj.sb]
			})
			
			this.add(viewer);

			if ((typeof this.layout) == "object") this.doLayout();

		}
		
	});

	/**
	 * 左侧变电站树
	 */
	business.equipment.BdzTree = Ext.extend(Ext.Panel, {
	
		/**
	     * 初始化
		 */
		initComponent : function() {
			
			var obj = this;
			
			this.tree = new Ext.tree.TreePanel({
				
	            id:"mytree",   
	            root : new Ext.tree.AsyncTreeNode({   
	        		id : "0", 
	        		draggable:false, 
	        		singleClickExpand:true, 
	        		text : treetitle
				}),   
				loader:new Ext.tree.TreeLoader({
	        		url : 'equip!getBdzTree.do?parentid=',
	        		listeners:{"beforeload":function(loader, node){ 
							loader.url ='equip!getBdzTree.do?parentid=' + node.id ;
	        		}}
	   		 	}),
		        useArrows:true,
		        autoScroll:true,
		        animate:true

			})
			
			this.tree.on({
	
				'click' : function(node){

					// 电压等级时
					if (node.id.toString().indexOf('kv') != -1) {
						obj.fireEvent('resetGrid');
					} else {
						obj.fireEvent('loadGrid', node);
					}

				}
				
			})
			
			// 注册事件
			this.addEvents(
				'loadGrid',
				'resetGrid'
			);

			this.items = [this.tree];
			
			business.equipment.BdzTree.superclass.initComponent.call(this);
			
		}
		
	});
	
	/**
	 * 右侧变电站设备
	 */
	business.equipment.BdzSb = Ext.extend(Ext.TabPanel, {
		
		/**
		 * 设备列表
		 */
		grid : null,
		
		/**
		 * 间隔列表数据源
		 */
		gridstore : null,

		/**
	     * 初始化
		 */
		initComponent : function() {
			
			var obj = this;

			//----------------------------------------------------------------------
			//	列表数据源
			//----------------------------------------------------------------------
			this.gridstore = new Ext.data.Store({
				proxy :new Ext.data.HttpProxy({
					url :'equip!getEquipList.do'
				}),
				reader :new Ext.data.JsonReader({
					root :'result',
					totalProperty :'totalCount',
					fields : ['id', 'bdzname', 'bdzid', 'typename', 'dydjname', 'sblxname', 'tyrq', 'state']
				}),
				autoLoad:false,
				remoteSort :false
			});

			//----------------------------------------------------------------------
			//	加载列表
			//----------------------------------------------------------------------
			this.grid = new Ext.grid.GridPanel({
				id					: "bdzsb_grid",
				store 				: obj.gridstore,
				region 				: 'west',
				enableDragDrop		: false,
				stripeRows			: true,
				title               : '设备信息列表',
				//split				: true,
				//collapsible			: true,
				animCollapse		: false,
				cm: new Ext.grid.ColumnModel({
	        		defaults: {
	            		sortable: true
	        		},
	        		columns: [
	        			new Ext.grid.RowNumberer(),
	            		//sm,
						{	id:'id',
							header :"序号",
							width :50,
							dataIndex :'id',
							sortable :true,
							hidden :true
						}, {
							header :"变电站",
							width :70,
							dataIndex :'bdzname',
							sortable :true,
							align:'center'
						}, {
							header :"隔断、瓶柜",
							width :70,
							dataIndex :'typename',
							sortable :true,
							align:'center'
						},{
							header :"设备类型",
							width :120,
							dataIndex :'sblxname',
							sortable :true,
							align:'center'
						},{
							header :"电压等级",
							dataIndex :'dydjname',
							sortable :true,
							align:'center'
						},{
							header :"投运日期",
							width :120,
							dataIndex :'tyrq',
							sortable :true,
							align:'center'
						}]
				}),
				selModel  : new Ext.grid.RowSelectionModel({singleSelect : true}),
				loadMask : {
					msg :'载入中,请稍候...'
				},
				viewConfig:{
					forceFit:true
				},
				tbar :[
					{
						id :'noteList_query',
						text :'刷新',
						cls:'x-btn-text-icon',
        				icon:contextpath + '/images/page_find.png',
			            handler:function() {
			            	obj.gridstore.reload();
        				}
					}, btn_insert ? '' : '-',{
						id :'btn_insert',
						text :'新建',
						cls:'x-btn-text-icon',
        				icon:contextpath + '/images/add.gif',
        				hidden:btn_insert
					}, btn_delete ? '' : '-',{
						id :'bdz_delete',
						text :'删除',
						cls:'x-btn-text-icon',
        				icon:contextpath + '/images/delete.gif',
        				hidden:btn_delete,
        				handler:function() {
        					obj.deleteSb();
        				}
					}
				],
				bbar : new Ext.PagingToolbar({
					store : obj.gridstore,
					pageSize:100,
					displayInfo :true,
					displayMsg :'共 {2} 条',
					emptyMsg :"没有数据显示！"
				}),
				listeners:{'rowdblclick':function(row) {
					obj.getDetail();
				}}
			});

			//----------------------------------------------------------------------
			//	列表事件
			//----------------------------------------------------------------------
			this.grid.addEvents(
				'addTab'
			);
			
			//----------------------------------------------------------------------
			//	加载新建设备类型菜单
			//----------------------------------------------------------------------
			var menudata = new Ext.data.Store({
								proxy : new Ext.data.HttpProxy({
								url : 'dic!getDicJson.do?dic.itemType=SBLX'
							}),
							reader : new Ext.data.JsonReader({}, ['itemId', 'itemName', 'remark']),
							autoLoad : false
						});
						
			menudata.load({	
    			callback: function(records, options, success){
    				
    				var arr = new Array();

    				menudata.each(function(item) {

    					//alert(menudata.getAt(0).get('itemName'));
    					
						var temp = new Ext.Action({
							id			: item.get('itemId'),
							text		: item.get('itemName'),
							handler		: function() { obj.insertSb(item.get('itemId'),item.get('itemName'))}
						});

						arr.push(temp);
					});

					Ext.getCmp('btn_insert').menu = new Ext.menu.Menu({ items : arr});
	
        		}
 
			});

			this.items = [
				this.grid
			]
			
			business.equipment.BdzSb.superclass.initComponent.call(this);
			
		},
	
		/**
		 * 菜单绑定
		 */
		menuCreate: function (){

			var obj = this;

			var actEdit = new Ext.Action({
				id			: 'menu_edit',
				text		: '修改',
				tooltip		: '修改当前间隔',
				iconCls		: 'icon_edit',
				handler		: function() { obj.getDetail()}
			});

			var actDelete = new Ext.Action({
				id			: 'menu_delete',
				text		: '删除',
				tooltip		: '删除当前间隔',
				iconCls		: 'icon_delete',
				hidden      : btn_delete,
				handler		: function() { obj.deleteJg()}
			});
			
			obj.commandMenu = new Ext.menu.Menu({
				minWidth	: 160,
				items	: [
					actEdit, '-',
					actDelete
				]
			});
			
			obj.grid.on('rowcontextmenu',function (grid, rowIndex, e) {
      			e.preventDefault();
      			obj.grid.getSelectionModel().selectRow(rowIndex);
        		obj.commandMenu.showAt(e.getXY());   
			});
			
		},

		/**
		 * 删除
		 */
		deleteSb: function() {
			
			var obj = this;
			
			if (!obj.grid.getSelectionModel().hasSelection()) {
				Ext.MessageBox.alert("提示","请先选择要删除的记录");
				return;
			}
			
			var ids = obj.grid.getSelectionModel().getSelected()['data']['id'];
			
			Ext.MessageBox.confirm('提示', '确定要删除选中的【' + obj.grid.getSelectionModel().getSelected()['data']['jgmc'] + '】记录吗？', function (btn) {
				
				if (btn == 'yes') {

					Ext.Ajax.request({
						method :'POST',
						url :'equip!deleteJg.do',
						params : {
							id :ids
						},
						success : function(response) {
							
							Ext.MessageBox.alert('提示', response.responseText, function () {
								obj.grid.store.reload();
							});
						},
						failure : function() {
							Ext.MessageBox.alert('提示', '删除失败！');
						}
					});
				}

			});
			
		},
		
		/**
		 * 新建时
		 */
		insertSb: function(sblx, sblxname) {
			alert();
			this.fireEvent('addTab', sblx, sblxname);

		}
	
	});